package com.zr.utils;

import com.zr.vo.LoginUser;
import com.zr.vo.sys.SysUser;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
public class TokenUtil {

    @Resource
    private RedisUtil redisUtil;

    public LoginUser currentUser(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("Authorization");
        Object obj = redisUtil.get(token);
        if(obj == null) throw new RuntimeException("Redis没有找到Token:" + token);
        return (LoginUser)obj;
    }

    public  String currentUserId(){
        return currentUser().getUserId();
    }

    public  String currentUserName(){
        return currentUser().getUserName();
    }

    public String token(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return  request.getHeader("Authorization");
    }

}
